#!/usr/bin/env python
#coding:utf-8

import cgi
import sqlite3
import time, sys
import cgitb;cgitb.enable()

conn = sqlite3.connect("log.sqlite")
curs = conn.cursor()

print "Content-type: text/html\n"
print "<html><body>"
print """\n
<h2>WyattWang's LOG</h2><hr>
<form method="post" action="index.py">
<input type="hidden" name="user" value="wyatt" />
<input type="hidden" name="time" value="%s" />
Content:<br />
<textarea name="content" cols="100" rows="10"></textarea><br />
<input type="submit" value="Submit" />
</form>
""" % time.ctime()

form = cgi.FieldStorage()
user = form.getvalue("user")
time = form.getvalue("time")
content = form.getvalue("content")

#if not content:
#    print "请输入内容，谢谢"
#    sys.exit()
if content:
    query = """insert into log(user, time, content) values('%s', '%s', '%s')""" % (user, time, content)
    curs.execute(query)
    conn.commit()

rec = curs.execute("select * from log order by id desc")

for i in rec.fetchall():
    #print "<p>ID: %s /Name: %s/Time: %s/Message:</p>%s" % (i[0], i[1], i[2], i[3])
    print i[3]
    print "<br />"

curs.close()
conn.close()

print "</body></html>"
